import { IVisitTask } from '@model/visit';
import { AbsFormUtil } from '@abstract/form';
import { Component, OnInit } from '@angular/core';
import { VisitService } from '@service/visit.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { FormBuilder } from '@angular/forms';
import { DatePipe } from '@angular/common';
import { NzModalService } from 'ng-zorro-antd/modal';
import { VisitTaskDetailComponent } from './visit-task-detail/visit-task-detail.component';
import { NzDrawerService } from 'ng-zorro-antd/drawer';
import { GriderList } from '@app/store/dict';
import { AuthService } from '@service/auth.service';

@Component({
	selector: 'app-visit-task',
	templateUrl: './visit-task.component.html',
	styleUrls: ['./visit-task.component.less'],
	providers: [DatePipe]
})
export class VisitTaskComponent extends AbsFormUtil implements OnInit {
	nzLoading = false;
	data = [];
	griderList = GriderList;
	constructor(
		private visitService: VisitService,
		private nzMessageService: NzMessageService,
		private fb: FormBuilder,
		private datePipe: DatePipe,
		private nzModalService: NzModalService,
		private nzDrawerService: NzDrawerService,
		private authService: AuthService

	) {
		super();
		this.authList = this.authService.getAuth();
	}

	ngOnInit() {
		this.buildForm();
		this.onSearch();
	}
	get griderTypeValue() {
		return this.getFormControl('griderType')?.value || null;
	}
	buildForm() {
		this.validateForm = this.fb.group({
			griderType: [1],
			name: [''],
			year: [new Date()],
			month: [new Date()],
			season: ['']
		});
	}

	async onSearch(isPageInit?: boolean) {
		this.nzLoading = true;
		if (isPageInit) {
			this.page.page = 1;
		}
		const formValue = this.validateForm.value;
		if (formValue.year) {
			formValue.year = this.datePipe.transform(formValue.year, 'yyyy') || '';
		}
		if (formValue.month) {
			formValue.month = this.datePipe.transform(formValue.month, 'yyyy-MM') || '';
		}
		const params = { ...formValue, ...this.page };
		const result = await this.visitService.getVisitList(params);
		this.data = result.data.list;
		this.page.total = result.data.totalCount;
		this.nzLoading = false;

	}





	detail(item: IVisitTask) {
		// const { name, year, season, month } = item;
		// const title = item.griderType === 1 ? `专职网格长${name} ${year ? year : ''}` : item.griderType === 2 ? `网格员${name} ${year ? year : ''} ${season ? season : ''} ` : `微网格长${name} ${month ? month:  ''} `
		const formValue = this.validateForm.value;
		if (formValue.year) {
			formValue.year = this.datePipe.transform(formValue.year, 'yyyy') || '';
		}
		if (formValue.month) {
			formValue.month = this.datePipe.transform(formValue.month, 'yyyy-MM') || '';
		}
		this.nzDrawerService.create({
			nzTitle: `走访详情`,
			nzContent: VisitTaskDetailComponent,
			nzWidth: '80%',
			nzContentParams: {
				formValue,
				id: item.id
			}
		});
	}
	remind(item: IVisitTask) {
		this.nzModalService.confirm({
			nzTitle: `对微网格长 ${item.name} 发送提醒吗？`,
			nzContent: '',
			nzOnOk: async () => {
				await this.visitService.warmVisit(item.id);
				this.nzMessageService.success('提醒成功');
			}
		});
	}

}
